import { Popover, Space, message } from 'antd';
import moment from 'moment'
import { ColumnType } from 'antd/lib/table'
import style from './index.module.less'
import { ServiceSchema, ServiceTopicSchema } from '@src/components/schema/service'
import { CaseSchema } from '@src/components/schema/caseSchema'
import { awaitWrap, confirmAction } from '@src/assets/js/tool'
import {getDetailRequest, updateModalData, sendElectronicRequest} from './ajax'

async function getDetail (id: string, callback: (v: ServiceSchema) => void) {
  const h = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getDetailRequest(id))
  h()
  if (e === null && d !== null) {
    callback(d)
  }
}

async function updateData (id: string, d: {[k: string]: any}, refreshFn: () => void) {
  const h = message.loading('提交中', 120)
  const [e] = await awaitWrap(updateModalData(id, d))
  h()
  if (e === null) {
    refreshFn()
  }
}

// 发送电子送达
async function sendElectronice (id: string, refreshFn: () => void) {
  const hide = message.loading('发送中', 120)
  const [e] = await awaitWrap(sendElectronicRequest(id))
  hide()
  if (e === null) {
    refreshFn()
  }
}

export function getColumns (
  theme: ServiceTopicSchema,
  caseDetail: CaseSchema,
  editable: boolean,
  showModify: (d: ServiceSchema | null) => void,
  showDetail: (d: ServiceSchema) => void,
  showResult: (d: ServiceSchema) => void,
  showFeedback: (d: ServiceSchema) => void,
  setRefreshListFn: (v: () => void) => void,
  refreshTable: () => void,
  createIdentity: (id: string, name: string) => void
):Array<ColumnType<ServiceSchema>>  {
  const list:Array<ColumnType<ServiceSchema>> = [
    {
      title: '序号',
      dataIndex: 'order',
      key: 'order'
    },
    {
      title: '收件人',
      dataIndex: 'contract',
      key: 'contract',
      render: (v, l) => {
        if (l.subject_names && v) {
          const content = (
            <div className={style.persons}>
              {(l.subject_names || '').split(',').map(li => (<div key={li}>{li}</div>))}
            </div>
          )
          return <Popover placement='top' title='当事人' content={content}><span className={style['has-agent']}>{v}</span></Popover>
        } else if (v) {
          return v
        }
        return l.structure
      }
    },
    {
      title: '送达方式',
      dataIndex: 'to_type',
      key: 'to_type',
      width: 100
    },
    {
      title: '邮箱',
      dataIndex: 'email',
      key: 'email'
    },
    {
      title: '联系电话',
      dataIndex: 'mobile',
      key: 'mobile'
    },
    {
      title: '送达情况',
      dataIndex: 'status',
      key: 'status',
      width: 100
    },
    {
      title: '发送时间',
      dataIndex: 'send_datetime',
      key: 'send_datetime',
      width: 180,
      render: (v) => {
        if (v) {
          return moment(v * 1000).format('YYYY-MM-DD HH:mm:ss')
        }
      }
    },
    {
      title: '送达时间',
      dataIndex: 'arrive_datetime',
      key: 'arrive_datetime',
      width: 180,
      render: (v) => {
        if (v) {
          return moment(v * 1000).format('YYYY-MM-DD HH:mm:ss')
        }
      }
    },
    {
      title: '操作',
      dataIndex: 'other',
      key: 'other',
      render: (v, l) => {
        const isSend = typeof l.send_datetime === 'number'
        if (isSend) {
          return (
            <Space>
              <span className={style.btn} onClick={() => getDetail(l.id, (d) => showDetail(d))}>查看详情</span>
              <span className={style.btn} onClick={() => {setRefreshListFn(refreshTable); getDetail(l.id, (d) => showResult(d))}}>发送情况</span>
              <span className={style.btn} onClick={() => {getDetail(l.id, (d) => showFeedback(d))}}>反馈内容</span>
              <span className={style.btn} onClick={() => { confirmAction('您确定要生成送达证明吗？', () => { createIdentity(l.id, l.contract) }) }}>送达证明</span>
            </Space>
          )
        }
        return (
          <Space>
            <span className={style.btn} onClick={() => { setRefreshListFn(refreshTable); getDetail(l.id, (d) => showModify(d)) }}>编辑</span>
            <span className={style.btn} onClick={() => confirmAction('您确定要发送电子送达吗？', () => {sendElectronice(l.id, refreshTable)})}>发送</span>
            <span className={style.btn} onClick={() => confirmAction('您确定要作废该送达吗？', () => {updateData(l.id, {status: '作废'}, refreshTable)})}>作废</span>
          </Space>
        )
      }
    }
  ]
  if (['自定义送达', '仲裁员送达'].indexOf(theme.type) === -1) {
    list.unshift({
      title: '仲裁地位',
      dataIndex: 'action_subject',
      key: 'action_subject'
    })
  }
  return list
}
